package com.zhiyou100.crm.servlet.notice;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.service.NoticeService;
import com.zhiyou100.crm.service.impl.NoticeServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.Notice;
import com.zhiyou100.crm.util.Pager;

/**
 * 通知公告列表
 */
@WebServlet("/notice/listview")
public class ListServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	NoticeService noticeService = new NoticeServiceImpl();
	List<Notice> notices;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String field = "";
		String keyword = "";
		int pageNo = 1;
		if ("POST".equalsIgnoreCase(request.getMethod())) {
			//获取post存入的值
			field = (String) request.getAttribute("field");
			keyword = (String) request.getAttribute("keyword");
			pageNo = (int) request.getAttribute("pageNo");
			notices=noticeService.queryNoticeByFile(keyword);
		} else {
			notices = noticeService.queryAllNotice();
		}
		//分页显示
		//初始页码
		int start = 1;
		//页面大小，设置
		int pageSize = 3;
		//获取当前页码
		String currentPage1 = request.getParameter("pageNo");
		if (currentPage1 != null&&currentPage1!="") {
			int currentPage2 = Integer.parseInt(currentPage1);
			pageNo = currentPage2;
		}
		//总数据数，集合的大小
		int total = notices.size();
		//总页数
		int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
		Pager pager = new Pager(total,pageNo,pageSize,pageCount,start);
		List<Notice> notices1 = new ArrayList<>();
		for (int i = (pageNo - 1) * pageSize; i < pageNo * pageSize; i++) {
			if (i == notices.size()) {
				break;
			}
			notices1.add(notices.get(i));
		}
//			request.setAttribute("notices", notices);
		request.setAttribute("pager", pager);
		request.setAttribute("notices", notices1);
		request.getRequestDispatcher("/WEB-INF/view/notice/listview.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		//搜索字段
		String field = request.getParameter("searchField");
		//关键字
		String keyword = request.getParameter("keyword");
		int pageNo=1;
		//主题编号
		String pn = request.getParameter("pageNo");
		if (pn != null && !"".equals(pn)) {
			pageNo = Integer.parseInt(pn);
		}
		request.setAttribute("field", field);
		request.setAttribute("keyword", keyword);
		request.setAttribute("pageNo", pageNo);
		doGet(request, response);
	}

}
